#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0, right = 0;
        int n = nums.size();
        int cnt = 0;
        int length = 0;
        for (; right < n; right++)
        {
            if (nums[right] == 0)
                cnt++;

            while (cnt > k)
            {
                if (nums[left++] == 0)
                    cnt--;
            }
            length = max(length, right - left + 1);
        }
        return length;
    }
};